﻿using CommonHelper.Model;
using CommonHelper.Server.CommonApi;
using CommonHelper.Utility;

namespace CommonHelper.Server
{
    /// <summary>
    /// 微信自定义菜单服务
    /// </summary>
    public class MenuService
    {
        private const string SearchMenuApi = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}";
        private const string CreateMenuApi = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}";
        private const string DeleteMenuApi = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}";

        private static string accessApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
        //public WxJsonResult CreateMenu(string accessToken)
        //{

        //}
        /// <summary>
        /// 查询菜单
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static string SearchMenu(string appId, string appSecret)
        {
            var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret);
            var url = string.Format(SearchMenuApi, accessToken);
            var json = HttpClientHelper.GetResponse(url);
            return JsonHelper.Encode(json);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static WxJsonResult Delete(string appId, string appSecret)
        {
            var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret);
            var url = string.Format(DeleteMenuApi, accessToken);
            return HttpClientHelper.GetResponse<WxJsonResult>(url);
        }
    }
}
